package com.example.administrator.timertask;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.administrator.timertask.util.TimeUtil;

/**
 * @author: njb
 * @date: 2020/3/7 0007 11:56
 * @desc:
 */
public class TimerActivity extends AppCompatActivity {
    private long seconds = 0;
    private Handler mHandler = new Handler();
    private boolean isPause = false;
    private TextView mTimerTv;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_countdown);
        initView();
        initHandler();
    }

    private void initView() {
        mTimerTv = findViewById(R.id.tv_time);
        mTimerTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(TimerActivity.this,CountDownActivity.class));
            }
        });
    }

    private void initHandler() {
        mHandler.post(timeRunnable);
    }

    private Runnable timeRunnable = new Runnable(){
        @Override
        public void run() {
            seconds = seconds+1000;
            Log.d("---currentSecond---",seconds +"");
            mTimerTv.setText(TimeUtil.getFormatHMS(seconds));
            String time = TimeUtil.getFormatHMS(seconds);
            Log.d("---time---",time +"");
            if (!isPause) {
                //递归调用本runable对象，实现每隔一秒一次执行任务
                mHandler.postDelayed(this, 1000);
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mHandler.removeCallbacksAndMessages(timeRunnable);
    }
}
